---
title: "Identifying Price Informativeness"
author: "Eduardo Davila^[Yale] & Cecilia Parlatore^[NYU Stern]"
date: "`r Sys.Date()`"
output:
  html_document: default
  pdf_document: default
---

```{r echo=FALSE, include=FALSE}

library(here); library(tidyverse); library(foreach); library(parallel); library(doSNOW); library(AER); library(lubridate); library(zoo);
path <- here::here(); print(path); setwd(path); rm(path)

load("intermediate/data_selected.RData")
load("intermediate/io_share.RData")
load("intermediate/parameters.RData")

source("functions/fn_identifying_stocks.R")
source("functions/fn_rolling_stocks.R")
source("functions/fn_panel_stocks.R")
source("functions/sc_ivol.R")

cl <- makeCluster(detectCores()-4, type="SOCK")
registerDoSNOW(cl)

```

# Quarterly, PS, Unlearnable

```{r}

df_input <- df_q_unlearnable %>% 
              mutate(year_qrt_calendar = as.yearqtr(as.yearmon(paste0(year,month), "%Y%m")),
                     year_calendar = lubridate::year(year_qrt_calendar),
                     month_calendar  = lubridate::month(year_qrt_calendar)+2) %>%
              left_join(df_io_share, by=c("permno", "year_calendar" =  "year", "month_calendar" =  "month")) %>%
              left_join(ivol, by=c("permno", "year","month"))

public_info_vars <- c(default_controls_levels, default_controls_logs)
unlearnable <- TRUE

attach(fn_panel_stocks_PS(df_input, public_info_vars, level_vars, log_vars, unlearnable, q_lag), warn.conflicts = F)
results_q_PS_unlearnable <- results
reg_long_eps_unlearnable <- reg_long_eps
reg_long_PS_unlearnable <- reg_long_PS
reg_short_eps_unlearnable <- reg_short_eps
reg_short_PS_unlearnable <- reg_short_PS

```

# Quarterly, PS, Term Structure

```{r}

tenors <- (df_q_tenors %>% distinct(tenor))$tenor

public_info_vars <- c(default_controls_levels, default_controls_logs)
unlearnable <- FALSE

results_q_PS_tenor <- c()

for(current_tenor in tenors){
  df_input <- df_q_tenors %>% 
              filter(tenor == current_tenor) %>% 
              mutate(year_qrt_calendar = as.yearqtr(as.yearmon(paste0(year,month), "%Y%m")),
                     year_calendar = lubridate::year(year_qrt_calendar),
                     month_calendar  = lubridate::month(year_qrt_calendar)+2) %>%
              left_join(df_io_share, by=c("permno", "year_calendar" =  "year", "month_calendar" =  "month")) %>%
              left_join(ivol, by=c("permno", "year","month"))

  attach(fn_panel_stocks_PS(df_input, public_info_vars, level_vars, log_vars, unlearnable, q_lag), warn.conflicts = F)
  results_q_PS_tenor <- rbind(results_q_PS_tenor, results)
}

```

# Quarterly, PS, No public

```{r}
df_input <- df_q %>% 
              mutate(year_qrt_calendar = as.yearqtr(as.yearmon(paste0(year,month), "%Y%m")),
                     year_calendar = lubridate::year(year_qrt_calendar),
                     month_calendar  = lubridate::month(year_qrt_calendar)+2) %>%
              left_join(df_io_share, by=c("permno", "year_calendar" =  "year", "month_calendar" =  "month")) %>%
              left_join(ivol, by=c("permno", "year","month"))

public_info_vars <- c()
unlearnable <- FALSE

attach(fn_panel_stocks_PS(df_input, public_info_vars, level_vars, log_vars, unlearnable, q_lag), warn.conflicts = F)
results_q_PS_nocon <- results

```

# Quarterly, PS

```{r}
df_input <- df_q %>% 
              mutate(year_qrt_calendar = as.yearqtr(as.yearmon(paste0(year,month), "%Y%m")),
                     year_calendar = lubridate::year(year_qrt_calendar),
                     month_calendar  = lubridate::month(year_qrt_calendar)+2) %>%
              left_join(df_io_share, by=c("permno", "year_calendar" =  "year", "month_calendar" =  "month")) %>%
              left_join(ivol, by=c("permno", "year","month"))

public_info_vars <- c(default_controls_levels, default_controls_logs)
unlearnable <- FALSE

attach(fn_panel_stocks_PS(df_input, public_info_vars, level_vars, log_vars, unlearnable, q_lag), warn.conflicts = F)
results_q_PS <- results

```

# Quarterly, Baseline
```{r}

df_input <- df_q
controls_levels <- default_controls_levels
controls_logs   <- default_controls_logs
unlearnable <- FALSE

results_q_rolling <- fn_rolling_stocks_par(df_input, controls_levels, controls_logs, window_T_q, unlearnable, q_lag)

```

# Quarterly, Unlearnable
```{r}

df_input <- df_q_unlearnable
controls_levels <- default_controls_levels
controls_logs   <- default_controls_logs
unlearnable <- TRUE

results_q_rolling_unlearnable <- fn_rolling_stocks_par(df_input, controls_levels, controls_logs, window_T_q, unlearnable, q_lag)

```

# Quarterly, Term Structure
```{r}
tenors <- (df_q_tenors %>% distinct(tenor))$tenor
controls_levels <- default_controls_levels
controls_logs   <- default_controls_logs
unlearnable <- FALSE

results_q_rolling_tenor <- c()

for(current_tenor in tenors){
  df_input <- df_q_tenors %>% filter(tenor == current_tenor)

  results_temp <- fn_rolling_stocks_new_par(df_input, controls_levels, controls_logs, window_T_q, unlearnable, q_lag)
  results_temp$tenor <- current_tenor
  results_q_rolling_tenor <- rbind(results_q_rolling_tenor, results_temp)
}

```

# Quarterly, No Controls

```{r}

df_input <- df_q
controls_levels <- c()
controls_logs   <- c()
unlearnable <- FALSE

results_q_rolling_nocon <- fn_rolling_stocks_par(df_input, controls_levels, controls_logs, window_T_q, unlearnable, q_lag)

```


# Save
```{r}

stopCluster(cl)

save(results_q_PS, results_q_PS_tenor, results_q_PS_unlearnable, results_q_PS_nocon, reg_long_PS, reg_short_PS, reg_long_eps, reg_short_eps, reg_long_PS_unlearnable, reg_short_PS_unlearnable, reg_long_eps_unlearnable, reg_short_eps_unlearnable , file = paste0("intermediate/results_PS.RData"))

save(results_q_rolling, results_q_rolling_unlearnable, results_q_rolling_tenor, results_q_rolling_nocon, file = paste0("intermediate/results_rolling.RData"))

```